﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Cleverscape.UTorrentClient.WebClient;
using uTorrentNetClient.ModelDesign.Helper;

namespace uTorrentNetClient.ModelDesign.Converter
{
    [ValueConversion(typeof(TorrentGroupsType), typeof(System.Windows.Media.ImageSource))]
    public class GroupImageDisplayConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(System.Windows.Media.ImageSource))
            {
                return null;
            }
            if (value is TorrentGroupsType)
            {
                TorrentGroupsType groupToConvert = (TorrentGroupsType)value;
                switch (groupToConvert)
                {
                    case TorrentGroupsType.All:
                        return BitmapToImageSource(Properties.Resources.all);
                    case TorrentGroupsType.Downloading:
                        return BitmapToImageSource(Properties.Resources.down);
                    case TorrentGroupsType.Ready:
                        return BitmapToImageSource(Properties.Resources.ready);
                    case TorrentGroupsType.Aktive:
                        return BitmapToImageSource(Properties.Resources.aktiv);
                    case TorrentGroupsType.NoAktive:
                        return BitmapToImageSource(Properties.Resources.inaktiv);
                    default:
                        break;
                }
            }
            return null;
        }

        private System.Windows.Media.ImageSource BitmapToImageSource(System.Drawing.Bitmap InputBitmap)
        {
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    InputBitmap.GetHbitmap(),
                    IntPtr.Zero,
                    System.Windows.Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
